package com.example.design.clean_craftsmanship

/**
 *Create by Fanketly on 2022/9/5
 *Gitee:https://gitee.com/fanketly
 *desc:
 */


enum class VideoType {
    REGULAR, CHILDREN
}

class Customer {
    var fee = 0.0
    var points = 0
    lateinit var movie: Movie



    fun getMovieFee() = movie.getFee()
    fun getMoviePoints() = movie.getPoints()

    fun addRental(videoType: VideoType, days: Int) {
        when (videoType) {
            VideoType.REGULAR -> {
                points += calculate(days, 2, 1.5) * 1
            }
            VideoType.CHILDREN -> {
                points += 1
                calculate(days, 0, 1.0)
            }
        }
    }

    private fun calculate(days: Int, freeDay: Int, f: Double): Int {
        val i1 = if (days in 1..freeDay + 1) 1 else days - freeDay
        for (i in 0 until i1) {
            fee += f
        }
        return i1
    }

}

/**
 *重构
 **/

interface Movie {
    fun getFee(): Double
    fun getPoints(): Int
}

class ChildrenMovie(private var days: Int) : Movie {
    override fun getFee(): Double {
        return days.toDouble()
    }

    override fun getPoints(): Int {
        return 1
    }

}

class RegularMovie(private var days: Int) : Movie {

    override fun getFee(): Double {
        var fee = 0.0
        val i1 = if (days in 1..3) 1 else days - 2
        for (i in 0 until i1) {
            fee += 1.5
        }
        return fee
    }

    override fun getPoints(): Int {
        return if (days in 1..3) 1 else days - 2
    }

}